<?php

namespace app\common\model\forest;

use app\common\model\User;
use app\common\model\user\Address;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'forest_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'plant_text',
        'pay_type_text',
        'refreshtime_text',
        'status_text',
        'image_str',
    ];
    public function getExpressComList()
    {
        return ['SF' => __('顺丰速运'), 'HTKY' => __('百世快递'), 'ZTO' => __('中通快递'), 'STO' => __('申通快递'), 'YTO' => __('圆通速递'), 'YD' => __('韵达速递'), 'YZPY' => __('邮政快递包裹'), 'EMS' => __('ems'), 'JD' => __('京东快递'), 'UC' => __('优速快递'), 'DBL' => __('德邦快递'), 'JTSD' => __('极兔速递'), 'ZYE' => __('众邮快递'), 'ZJS' => __('宅急送')];
    }
    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }

    public function getPlantList()
    {
        return ['0' => __('Plant 0'), '1' => __('Plant 1')];
    }

    public function getPayTypeList()
    {
        return ['0' => __('Pay_type 0'), '1' => __('Pay_type 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getPlantTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['plant']) ? $data['plant'] : '');
        $list = $this->getPlantList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefreshtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refreshtime']) ? $data['refreshtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setRefreshtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function address()
    {
        return $this->belongsTo('app\common\model\user\Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function yuOrder($id, $address_id, $uid)
    {
        $goods = new Member();
        $goods = $goods->get($id);
        if (isset($address_id) && !empty($address_id)) {
            $addressInfo = Address::get($address_id);
            $goods->addressInfo = $addressInfo;
            $goods->address_id = $address_id;
        } else {
            $addressInfo = Address::getDefaultAddress($uid);
            if ($addressInfo) {
                $goods->addressInfo = $addressInfo;
                $goods->address_id = $addressInfo['id'];
            }
        }
        $goods->pay_price = $goods->price + $goods->freight;
        $user = User::get($uid);
        if ($user->forest_level < $goods->id) {
            $level = Member::get($user->forest_level);
            if ($level) {
                $goods->pay_price = $goods->pay_price - $level->price;
            }
        }
        return $goods->toArray();
    }
    public  function addData($data,  $uid)
    {
        $addressInfo = [];
        $address = '';
        if (isset($data['addressInfo']) && !empty($data['addressInfo'])) {
            $addressInfo = $data['addressInfo'];
            $address =  $addressInfo['province'] . $addressInfo['city'] . $addressInfo['district'] . $addressInfo['address'];
        }
        $array = [
            'user_id' => $uid,
            'name' =>  $data['title'],
            'image' =>  $data['image'],
            'pay_price' =>  $data['pay_price'],
            'price' =>  $data['price'],
            'freight' =>  $data['freight'],
            'sn' => sn('forest_order', 'sn', 'FO'),
            'plant' => $data['plant'],
            'address_id' => $data['address_id'] ?? 0,
            'address' => $address,
            'pay_type' => $data['pay_type'],
            'consignee' => $addressInfo['contact'] ?? '',
            'phone' => $addressInfo['telephone'] ?? '',
            'goods_id' => $data['id'] ?? 0,
        ];
        $this->allowField(true)->save($array);
        return $this->id;
    }
}
